﻿Partial Public Class TunnelingEvents

    Private _objRoutedEventHandler As RoutedEventHandler = New RoutedEventHandler(AddressOf innerGrid_PreviewMouseDown)

    Private Sub outerGrid_PreviewMouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles outerGrid.PreviewMouseDown
        Me.outerGrid.Background = Brushes.Red
        e.Handled = Me.chkOuter.IsChecked.Value
    End Sub

    Private Sub TunnelingEvents_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        RestoreColors()
        Me.innerGrid.AddHandler(Grid.PreviewMouseDownEvent, _objRoutedEventHandler)
    End Sub

    Private Sub TunnelingEvents_PreviewMouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.PreviewMouseUp
        RestoreColors()
    End Sub

    Private Sub RestoreColors()
        Me.outerGrid.Background = Brushes.LightYellow
        Me.middleGrid.Background = Brushes.LightGreen
        Me.innerGrid.Background = Brushes.LightGray
    End Sub

    Private Sub middleGrid_PreviewMouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles middleGrid.PreviewMouseDown
        Me.middleGrid.Background = Brushes.Red
        e.Handled = Me.chkMiddle.IsChecked.Value
    End Sub

    Private Sub innerGrid_PreviewMouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Me.innerGrid.Background = Brushes.Red
        e.Handled = Me.chkInner.IsChecked.Value
    End Sub

    Private Sub chkInner_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles chkInner.Checked
        Me.innerGrid.RemoveHandler(Grid.PreviewMouseDownEvent, _objRoutedEventHandler)
        Me.innerGrid.AddHandler(Grid.PreviewMouseDownEvent, _objRoutedEventHandler, True)
    End Sub

    Private Sub chkInner_Unchecked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles chkInner.Unchecked
        Me.innerGrid.RemoveHandler(Grid.PreviewMouseDownEvent, _objRoutedEventHandler)
        Me.innerGrid.AddHandler(Grid.PreviewMouseDownEvent, _objRoutedEventHandler)
    End Sub
End Class
